/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2016-3-22         yellowlgx
 *
 */

package com.skyworth.ui.customview.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.skyworth.commen.ui.R;
import com.skyworth.theme.SkyThemeEngine;
import com.skyworth.ui.define.SkyTextSize;
import com.skyworth.ui.listview.AdapterItem;
import com.skyworth.ui.listview.MetroListItem;
import com.skyworth.util.SkyScreenParams;

/**
 * @ClassName LocalAppItemView
 * @Description TODO (write something)
 * @author yellowlgx
 * @date 2016-3-22
 * @version TODO (write something)
 */
@SuppressLint("RtlHardcoded")
public class CustomAppItemView extends MetroListItem implements AdapterItem<CustomAppInfo>
{
    private Context mContext;
    private CustomAppInfo curInfo;
    private ImageView bgView;
    private ImageView iconView;
    private TextView titleView;
    private ImageView rightFlagView;

    private FrameLayout mShaderLayout;

    public CustomAppItemView(Context context)
    {
        super(context);
        mContext = context;
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
        initView();
    }

    private void initView()
    {
        int padding = getResolutionValue(6);
        this.setPadding(padding, padding, padding, padding);

        mShaderLayout = new FrameLayout(mContext);
        mShaderLayout.setBackgroundDrawable(SkyThemeEngine.getInstance()
                .getDrawable(R.drawable.ui_sdk_btn_unfocus_big_shadow, false));
        this.addView(mShaderLayout);

        bgView = new ImageView(mContext);
        iconView = new ImageView(mContext);
        titleView = new TextView(mContext);
        titleView.setTextColor(getResources().getColor(R.color.c_3));
        titleView.setTextSize(getTextDpiValue(SkyTextSize.t_2));
        titleView.setGravity(Gravity.CENTER);
        titleView.setSingleLine();
        titleView.setEllipsize(TruncateAt.MARQUEE);
        titleView.setMarqueeRepeatLimit(-1);

        rightFlagView = new ImageView(mContext);

        FrameLayout.LayoutParams bg_p = new FrameLayout.LayoutParams(getResolutionValue(238),
                getResolutionValue(238));
        mShaderLayout.addView(bgView, bg_p);

        FrameLayout.LayoutParams icon_p = new FrameLayout.LayoutParams(getResolutionValue(145),
                getResolutionValue(145));
        icon_p.gravity = Gravity.CENTER_HORIZONTAL;
        icon_p.topMargin = getResolutionValue(30);
        mShaderLayout.addView(iconView, icon_p);
        FrameLayout.LayoutParams title_p = new FrameLayout.LayoutParams(getResolutionValue(185),
                LayoutParams.WRAP_CONTENT);
        title_p.topMargin = getResolutionValue(185);
        title_p.gravity = Gravity.CENTER_HORIZONTAL;
        mShaderLayout.addView(titleView, title_p);

        FrameLayout.LayoutParams flag_p = new FrameLayout.LayoutParams(getResolutionValue(48),
                getResolutionValue(48));
        flag_p.gravity = Gravity.RIGHT | Gravity.TOP;
        mShaderLayout.addView(rightFlagView, flag_p);
        rightFlagView.setVisibility(View.INVISIBLE);
    }

    public void refreshOnThemeChanged()
    {
        if (this.hasFocus())
        {
            titleView.setTextColor(getResources().getColor(R.color.a_5));
        } else
        {
            titleView.setTextColor(getResources().getColor(R.color.c_3));
        }
    }

    @Override
    public View getLayout()
    {
        return this;
    }

    @Override
    public void onUpdateItemValue(CustomAppInfo model, int position, int viewType)
    {
        curInfo = model;
        mViewType = viewType;
    }

    @Override
    public void clearItem()
    {
        if (iconView != null)
            iconView.setBackgroundColor(Color.TRANSPARENT);

        if (titleView != null)
            titleView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void refreshView()
    {
        if (curInfo != null)
        {
            iconView.setBackgroundDrawable(curInfo.icon);
            titleView.setText(curInfo.title);
            titleView.setVisibility(View.VISIBLE);

            setRightFlagState(curInfo.isChoosed);
        }
    }

    public CustomAppInfo getCurInfo()
    {
        return curInfo;
    }

    public void setRightFlagState(boolean isChoosed)
    {
        curInfo.isChoosed = isChoosed;
        if (isChoosed)
        {
            rightFlagView.setImageResource(R.drawable.ic_custom_item_selected);
            rightFlagView.setVisibility(View.VISIBLE);
        } else
        {
            rightFlagView.setImageResource(R.drawable.ic_custom_item_unselected);
        }
    }

    public void focus()
    {
        titleView.setSelected(true);
        titleView.setTextColor(getResources().getColor(R.color.a_5));
        bgView.setBackgroundColor(Color.WHITE);
        rightFlagView.setVisibility(View.VISIBLE);
    }

    public void unfocus()
    {
        titleView.setSelected(false);
        titleView.setTextColor(getResources().getColor(R.color.c_3));
        bgView.setBackgroundColor(Color.TRANSPARENT);
        if (curInfo.isChoosed)
        {
            rightFlagView.setVisibility(View.VISIBLE);
        } else
        {
            rightFlagView.setVisibility(View.INVISIBLE);
        }
    }

    public int getResolutionValue(int value)
    {
        return SkyScreenParams.getInstence(mContext).getResolutionValue(value);
    }

    public int getTextDpiValue(int textSize)
    {
        return SkyScreenParams.getInstence(mContext).getTextDpiValue(textSize);
    }

}
